define('views/index', ['text!/views/index.html'], function(template) {
  var new_public_list_size = 40;
  var new_public_list_page_length = 9;
  return {
    template: template,
    replace: true,
    data: function() {
      return {
        init: true,
        list: [],
        total: 0,
        pageSize: new_public_list_size,
        pageNumber: this.$route.query.pageNumber || 0,
        minPage: 0,
        maxPage: 0,
        pageLength: new_public_list_page_length,
        lastPage: 0,
        cid: this.$route.query.cid || 0
      };
    },
    methods: {
      pageTo: function(page) {
        this.$router.go({
          name: 'index',
          query: {
            pageNumber: page,
            cid: this.cid
          }
        });
      },
      getNewPublicList: getNewPublicList
    },
    route: {
      data: function(transition) {
        var query = transition.to.query;
        this.pageNumber = 'pageNumber' in query ? query.pageNumber : 0;
        this.cid = 'cid' in query ? query.cid : 0;
        this.getNewPublicList();
        transition.next();
      }
    },
    props: ['user', 'box']
  };

  function getNewPublicList() {
    var vm = this;
    vm.$http.post('/item/getNewPublicList', {
      pageSize: new_public_list_size,
      pageNumber: vm.pageNumber,
      cid: vm.cid
    }).then(function(res) {
      res = res.data
      if (res.code !== 0) {
        console.log(res.message);
        return;
      }
      vm.cid = res.data.cid;
      vm.list = res.data.list;
      vm.total = res.data.total;
      vm.pageNumber = res.data.pageNumber;
      vm.pageSize = res.data.pageSize;
      vm.lastPage = Math.floor(res.data.total / res.data.pageSize);

      vm.pageLength = Math.min(new_public_list_page_length, vm.lastPage);
      vm.minPage = Math.max(0, res.data.pageNumber - Math.floor(new_public_list_page_length / 2));
      vm.maxPage = Math.min(vm.lastPage, res.data.pageNumber + Math.floor(new_public_list_page_length / 2));
      window.scrollTo(0, 0);
    });
  }
});